<template id="tpl">
  <div class="my-cover">
    <div class="operateBox" @click="dialogVisible = true">
      <span>选择素材</span>
      <img :src="picUrl?picUrl:null"  class="img" />
    </div>
    <!-- 对话框 -->
    <el-dialog :append-to-body="true" :visible.sync="dialogVisible" width="720px">
      <el-card class="box-card">
        <div style="padding-bottom: 20px;">
          <el-radio-group v-model="collect" size="mini" @change="collectChange">
            <el-radio-button :label="false">全部</el-radio-button>
            <el-radio-button :label="true">收藏</el-radio-button>
          </el-radio-group>
        </div>
        <el-row :gutter="10">
          <el-col
            v-for="(image) in imageList"
            :key="image.id"
            class="img_item"
            :xs="12"
            :sm="6"
            :md="6"
            :lg="4"
          >
            <div class="img_box" @click="selectHandle(image)">
              <img :src="image.url" alt />
               <div class="selected" v-if="(image.isSelected)">
                <img src="./selected.png" alt="">
              </div>
            </div>
          </el-col>
        </el-row>
        <el-pagination
          style="margin-top:10px;"
          background
          layout="sizes, prev, pager, next"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="10"
          @current-change="currentChange"
          @size-change="sizeChange"
          :total="total_cont"
        ></el-pagination>
      </el-card>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 引入图片素材接口
import { getImageList } from '@/api/image'

export default {
  name: 'Cover',
  data () {
    return {
      imageList: [], // 素材列表
      total_cont: 0, // 素材总数
      curPage: 1, // 当前要请求第几页数据
      per_page: 10, // 当前每页的条数
      dialogVisible: false, // 弹出框是否显示
      collect: false, // 是否为收藏状态
      curImage: '' // 当前选中的图片
    }
  },
  methods: {
    openDialog () {
      this.dialogVisible = true
    },
    currentChange (pageNum) {
      this.curPage = pageNum
      this.hGetImage()
    },

    hGetImage () { // 请求素材列表
      const params = {
        collect: this.collect,
        per_page: this.per_page, // 每页的条数
        page: this.curPage// 当前请求第几页
      }
      getImageList(params).then(res => {
        // 这里控制勾选状态的显示
        const sourceData = res.data.data.results
        this.imageList = sourceData.map((item) => {
          return { // 遍历返回一个处理完的新素材列表赋值给data里的img数据
            ...item,
            isSelected: false
          }
        })
        this.total_cont = res.data.data.total_count// 总条数
      })
    },
    sizeChange (pageSize) {
      this.per_page = pageSize
      this.hGetImage()
    },
    collectChange (collect) {
      this.hGetImage()
    },
    cancel () { // 取消点击事件
      this.$emit('update:picUrl', '')
      this.dialogVisible = false
    },
    confirm () { // 确定点击事件:把选好的数据提交到父组件中
      this.$emit('update:picUrl', this.curImage.url)
      this.dialogVisible = false // 关闭对话框
    },
    selectHandle (cur) { // 选中素材
      this.imageList.forEach((item) => {
        item.isSelected = false
      })
      cur.isSelected = true// 将当前项中的isSelected改为true
      this.curImage = cur // 将选中的素材赋值给cur 留作后用
    }
  },
  props: {
    picUrl: {
      type: [String, Array]
    }
  },
  mounted () {
    this.hGetImage()
  }
}
</script>

<style lang="less" scoped>
img:not([src]){
  opacity: 0;
}

.my-cover {
  display: inline-block;
}
.operateBox {
  display: inline-block;
  width: 100px;
  height: 100px;
  margin-left: 10px;
  border-radius: 4px;
  border: 1px solid #ccc;
  font-size: 13px;
  color: #666;
  text-align: center;
  line-height: 100px;
  overflow: hidden;
  cursor: pointer;
  position: relative;
  .img {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }
}
.img_item {
  position: relative;
  box-sizing: border-box;
  .img_box {
    height: 120px;
    margin-bottom: 10px;
    position: relative;
    img {
      width: 100%;
      height: 100%;
    }
    .selected {
      position: absolute;
      width: 20%;
      left: 0;
      top: 0;
    }
  }
}
.option {
  position: absolute;
  left: 5px;
  right: 5px;
  bottom: 5px;
  height: 30px;
  background: rgba(0, 0, 0, 0.3);
  color: #fff;
  display: flex;
  span {
    flex: 1;
    text-align: center;
    cursor: pointer;
    line-height: 30px;
  }
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.uploadBox {
  text-align: center;
  img {
    width: 100%;
  }
  // scoped代表当前的样式只能影响当前的组件  影响不了子组件的样式
  // 加上 /deep/ 之后可以进行深度样式控制
  // 当试图直接控制子组件内部元素样式的时候就需要添加 /deep/
  /deep/.el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
}
</style>
